package game;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;



/**
 * A standard object that can be placed inside a game
 * @author Twisty
 */
public class GameObject {
	public enum State{
		NORMAL, DEAD
	}
	private int respawnTime = 150; //Respawn time
	private double x, y; //Coordinates in map
	private int width, height; //Dimension of the object
	private double rotation; //The current rotation in 360 degrees
	private boolean active; //Whether the object is active in the game or not
	public State state = State.NORMAL;
	public int currentRespawnTime = respawnTime;

	/**
	 * @param x - x starting coordinate
	 * @param y - y starting coordinate
	 */
	public GameObject(int x, int y){
		this.x = x;
		this.y = y;
		this.setActive(true);
	}
	
	/**
	 * @return - the current path to the current image
	 */
	public String curImgPath(){
		return "";
	}
	
	/**
	 * @param respawnTime - The steps it takes to respawn
	 */
	public void setRespawnTime(int respawnTime){
		this.respawnTime = respawnTime;
		this.currentRespawnTime = respawnTime;
	}
	
	/**
	 * @return - the current steps it takes to respawn
	 */
	public int getRespawnTime(){
		return respawnTime;
	}
	
	/**
	 * @param newRotation - sets the new rotation
	 */
	public void setRotation(double newRotation){
		if(newRotation > 360){
			newRotation = (newRotation%360); //Makes sure the rotation isn't > 360
		}
		this.rotation = newRotation;
	}
	
	/**
	 * @param addedRotation - adds amount to the current rotation
	 */
	public void addRotation(double addedRotation){
		this.rotation += addedRotation;
		if(this.rotation > 360){
			this.rotation = (this.rotation%360); //Makes sure the rotation isn't > 360
		}
	}
	
	/**
	 * @return - returns the current rotation
	 */
	public double getRotation(){
		return this.rotation;
	}
	
	/**
	 * @return - returns the x coordinate
	 */
	public int getX(){
		return (int)this.x;
	}
	
	/**
	 * @return - returns the y coordinate
	 */
	public int getY(){
		return (int)this.y;
	}
	
	/**
	 * @return - returns the height of the object
	 */
	public int getHeight(){
		return this.height;
	}
	
	/**
	 * @return - returns the width of the object
	 */
	public int getWidth(){
		return this.width;
	}
	
	/**
	 * @param newWidth - the new width
	 */
	public void setWidth(int newWidth){
		this.width = newWidth;
	}
	
	/**
	 * @param newHeight - the new height
	 */
	public void setHeight(int newHeight){
		this.height = newHeight;
	}
	
	/**
	 * @param x - sets the object's x to the given x
	 * @param y - sets the object's y to the given y
	 */
	public void setCoordinates(int x, int y){
		this.x = x;
		this.y = y;
	}
	
	/**
	 * @param xAmount - adds an amount to the current x coordinate
	 * @param yAmount - adds an amount to the current y coordinate
	 */
	public void addCoordinates(double xAmount, double yAmount){
		this.x += xAmount;
		this.y += yAmount;
	}
	
	/**
	 * Sets the object on active
	 * @param active - true or false
	 */
	public void setActive(boolean active){
		this.active = active;
	}
	
	/**
	 * Returns whether the object is active
	 * @return - true if active
	 */
	public boolean isActive(){
		return this.active;
	}
	
	/**
	 * Respawns game object on a new random point on the map
	 * @param map
	 */
	public void Respawn(GameMap map){
		boolean respawned = false;
		while(!respawned){
			int ranX = (int)(Math.random()*(map.getWidth()-this.getWidth()));
			int ranY = (int)(Math.random()*(map.getHeight()-this.getHeight()));
			int colPoints = 0;
			
			for(int x = ranX; x < ranX+this.getWidth(); x++){
				for(int y = ranY; y < ranY+this.getWidth(); y++){
					if(map.hasPointAtLocation(x, y)){
						colPoints++;
					}
				}
			}
			if(colPoints == 0) {
				respawned = true;
				this.setCoordinates(ranX, ranY);
				this.setActive(true);
				this.state = State.NORMAL;
				PostRespawn();
			}
		}
	}
	
	/**
	 * Function that can be overridden for functionalities after respawning
	 */
	public void PostRespawn(){
	}
}
